<template>
	<view class="placeOrder">
		<view @click="addresschoose" class="placeOrder_address">
			<view class="placeOrder_address_left">
				<image src="../../../static/home/addressicon.png" mode="widthFix"></image>
			</view>
			<view v-if="shippingAddress == null" class="placeOrder_address_right">
				<view class="placeOrder_address_right_left">
					<view class="placeOrder_address_right_title">
						<view style="width: 100%;" class="placeOrder_address_right_title_left">
							请填写收货地址
						</view>
					</view>
				</view>
				<view class="placeOrder_address_right_bottom_right">
					<image src="../../../static/home/right.png" mode="widthFix"></image>
				</view>
			</view>
			<view v-if="shippingAddress !== null" class="placeOrder_address_right">
				<view class="placeOrder_address_right_left">
					<view class="placeOrder_address_right_title">
						<view class="placeOrder_address_right_title_left">
							{{shippingAddress.name}}
						</view>
						<view class="placeOrder_address_right_title_right">
							{{shippingAddress.creator}}
						</view>
					</view>
					<view class="placeOrder_address_right_bottom">
						{{shippingAddress.allAddressInfo}}
					</view>
				</view>
				<view class="placeOrder_address_right_bottom_right">
					<image src="../../../static/home/right.png" mode="widthFix"></image>
				</view>
			</view>
		</view>

		<view v-for="(item,index) in commoditylist" class="placeOrder_commodity">
			<view class="placeOrder_commodity_detail">
				<view class="placeOrder_commodity_detail_left">
					<image :src="item.goodsImages" mode=""></image>
				</view>
				<view class="placeOrder_commodity_detail_right">
					<view class="placeOrder_commodity_detail_right_left">
						<view class="placeOrder_commodity_detail_right_title">
							{{item.goodsName}}
						</view>
						<view class="placeOrder_commodity_detail_right_bottom">
							<text>{{item.goodsSpecificationsNames}}</text>
						</view>
					</view>
					<view class="placeOrder_commodity_detail_right_right">
						<view class="placeOrder_commodity_detail_right_rightnum">
							<text class="placeOrder_commodity_detail_right_rightnum_left">￥</text><text
								v-if="user.isVip==1">{{item.goodsMemberPrice}}</text><text
								v-if="user.isVip==0">{{item.goodsPrice}}</text>
						</view>
						<view class="placeOrder_commodity_detail_right_rightqua">
							×{{item.num }}
						</view>
					</view>
				</view>
			</view>
			<view class="placeOrder_commodity_quantity">
				<view class="placeOrder_commodity_quantity_left">
					购买数量
				</view>
				<view class="placeOrder_commodity_quantity_right">
					<u-number-box min="1" v-model="item.num"></u-number-box>
				</view>
			</view>
			<view class="placeOrder_commodity_quantity">
				<view class="placeOrder_commodity_quantity_left">
					订单备注
				</view>
				<view class="placeOrder_commodity_quantity_right">
					<u--textarea style="width: 100%;padding: 0;" v-model="item.remarks" placeholder="请输入订单备注">
					</u--textarea>
				</view>
			</view>
		</view>

		<view class="placeOrder_buttonBox">
			<view class="placeOrder_buttonBox_box">
				<view class="placeOrder_buttonBox_box_left">
					共{{numFun()}}件，
				</view>
				<view class="placeOrder_buttonBox_boxNum">
					合计：<text class="placeOrder_buttonBox_boxNum_left">￥</text><text v-if="user.isVip == 1"
						class="placeOrder_buttonBox_boxNum_right">{{memberPrice()}}</text><text v-if="user.isVip == 0"
						class="placeOrder_buttonBox_boxNum_right">{{price()}}</text>
				</view>
				<view class="placeOrder_buttonBox_buttonBox">
					<button :disabled="buttonLoading" @click="confirm" class="placeOrder_buttonBox_button">提交</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		bootreceivingAddressselectdefault,
		// bootgoodsselectdetail,
		// bootorderinsert
		bootshoppingCartselectlist,
		bootorderinsertBatch
	} from '@/api/home'
	export default {
		data() {
			return {
				user: JSON.parse(uni.getStorageSync('user')),
				form: {},
				goosform: null,
				shippingAddress: null,
				num: 1,
				remarks: '',
				buttonLoading: false,
				commoditylist: []
			}
		},
		onLoad(data) {
			console.log(this.user)
			this.bootshoppingCartselectlist(JSON.parse(data.list))
			// this.form = JSON.parse(data.data)
			this.bootreceivingAddressselectdefault()
			// this.bootgoodsselectdetail()
			// if(user)
		},
		onShow() {
			let address = uni.getStorageSync('addresschoose')
			if (address) {
				let obj = JSON.parse(address);
				this.shippingAddress = obj
				uni.setStorageSync('addresschoose', undefined);
			}
		},
		methods: {
			numFun() {
				let num = 0;
				for (var i = 0; i < this.commoditylist.length; i++) {
					let elem = this.commoditylist[i]
					num += elem.num
				}
				return num
			},
			memberPrice() {
				let num = 0;
				for (var i = 0; i < this.commoditylist.length; i++) {
					let elem = this.commoditylist[i]
					num += (elem.goodsMemberPrice * elem.num)
				}
				return num
			},
			price() {
				let num = 0;
				for (var i = 0; i < this.commoditylist.length; i++) {
					let elem = this.commoditylist[i]
					num += (elem.goodsPrice * elem.num)
				}
				return num

			},
			bootshoppingCartselectlist(data) {
				bootshoppingCartselectlist(data).then(res => {
					this.commoditylist = res.data
				})
			},
			addresschoose() {
				uni.navigateTo({
					url: '/pages/mine/AddressManagement?type=2'
				});
			},
			bootgoodsselectdetail() {
				bootgoodsselectdetail(this.form.goodsId).then(res => {
					this.goosform = res.data
				})
			},
			confirm() {
				if (this.shippingAddress == null) {
					this.addresschoose()
					return
				}
				let arr = []
				for (var i = 0; i < this.commoditylist.length; i++) {
					let elem = this.commoditylist[i]
					arr.push({
						goodsId: elem.goodsId,
						goodsSpecificationsValue: elem.goodsSpecificationsNames,
						num: elem.num,
						receivingAddress: this.shippingAddress.allAddressInfo,
						receivingAddressName: this.shippingAddress.name,
						receivingAddressPhone: this.shippingAddress.phone,
						goodsSpecificationsIds: elem.goodsSpecificationsIds,
						remarks: elem.remarks
					})
				}
				this.buttonLoading = true
				bootorderinsertBatch(arr).then(res => {
					if (res.success == 1) {
						console.log(res.data)
						let arr = [];
						for (var i = 0; i < res.data.length; i++) {
							let em = res.data[i];
							arr.push(em.id)
						}

						uni.redirectTo({
							url: '/pages/index/shoppingCartpayment/shoppingCartpayment?ids=' + JSON
								.stringify(arr) + '&orderNo=' + res.data[0].orderNo
						});
					}else{
						uni.showToast({
							icon: 'none',
							title: res.message
						})
					}
				}).finally(res => {
					this.buttonLoading = false
					// console.log(res);
				})
			},
			bootreceivingAddressselectdefault() {
				bootreceivingAddressselectdefault().then(res => {
					console.log(res)
					this.shippingAddress = res.data
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	* {
		box-sizing: border-box;
	}

	.placeOrder {
		padding: 20upx;

		.placeOrder_address {
			display: flex;
			align-items: center;
			background-color: #ffffff;
			border-radius: 20upx;
			padding: 20upx 20upx;

			.placeOrder_address_left {
				width: 100upx;
				margin-right: 20upx;

				image {
					width: 100%;
				}
			}

			.placeOrder_address_right {
				width: calc(100% - 20upx);
				display: flex;
				align-items: center;

				.placeOrder_address_right_left {
					width: calc(100% - 20upx - 18upx);
					margin-right: 20upx;

					.placeOrder_address_right_title {
						display: flex;
						align-items: center;

						.placeOrder_address_right_title_left {
							width: 220upx;
							overflow: hidden;
							text-overflow: ellipsis;
							white-space: nowrap;
							font-size: 36upx;
							color: rgba(0, 0, 0, 0.8);
						}

						.placeOrder_address_right_title_right {
							font-size: 28upx;
							font-family: PingFangSC-Regular, PingFang SC;
							font-weight: 400;
							color: rgba(0, 0, 0, 0.54);
							margin-left: 20upx;
						}
					}

					.placeOrder_address_right_bottom {
						display: flex;
						align-items: center;

						// width: calc(100% - 18upx - 20upx);
						// margin-right: 20upx;
						text-overflow: -o-ellipsis-lastline;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 2;
						line-clamp: 2;
						font-size: 28upx;
						-webkit-box-orient: vertical;


					}
				}

				.placeOrder_address_right_bottom_right {
					width: 18upx;
					font-size: 0;

					image {
						width: 18upx;
					}
				}
			}
		}

		.placeOrder_commodity {
			margin-top: 20upx;
			background-color: #ffffff;
			border-radius: 20upx;
			padding: 20upx 20upx;

			.placeOrder_commodity_detail {
				display: flex;

				.placeOrder_commodity_detail_left {
					width: 180upx;
					height: 180upx;

					image {
						width: 100%;
						border-radius: 10upx;
						height: 100%;
					}
				}

				.placeOrder_commodity_detail_right {
					width: calc(100% - 180upx - 20upx);
					margin-left: 20upx;
					display: flex;

					.placeOrder_commodity_detail_right_left {
						width: calc(100% - 120upx - 20upx);
						margin-right: 20upx;

						.placeOrder_commodity_detail_right_title {
							text-overflow: -o-ellipsis-lastline;
							overflow: hidden;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-line-clamp: 2;
							line-clamp: 2;
							-webkit-box-orient: vertical;
							font-size: 28upx;
						}

						.placeOrder_commodity_detail_right_bottom {
							font-size: 28upx;
							padding: 6upx;
							color: #999;
							background-color: #f5f5f5;
						}
					}

					.placeOrder_commodity_detail_right_right {
						width: 120upx;

						.placeOrder_commodity_detail_right_rightnum {
							font-size: 28upx;
							color: #000000;
							text-align: right;

							.placeOrder_commodity_detail_right_rightnum_left {
								font-size: 24upx;
								margin-right: 5upx;
							}
						}

						.placeOrder_commodity_detail_right_rightqua {
							font-size: 28upx;
							color: #ccc;
							text-align: right;
							margin-top: 10upx;
						}
					}
				}

			}

			.placeOrder_commodity_quantity {
				display: flex;
				// align-items: ;
				justify-content: space-between;
				margin-top: 20upx;

				.placeOrder_commodity_quantity_left {
					font-size: 30upx;
					width: 160upx;
					text-align: right;
				}

				.placeOrder_commodity_quantity_right {
					width: calc(100% - 160upx - 20upx);
					display: flex;
					justify-content: flex-end;
				}
			}
		}

		.placeOrder_buttonBox {
			position: fixed;
			bottom: 0;
			padding: 20upx;
			height: 170upx;
			width: 100%;
			left: 0;
			display: flex;
			justify-content: flex-end;
			background-color: #fff;
			align-items: center;

			.placeOrder_buttonBox_box {
				display: flex;
				align-items: center;

				.placeOrder_buttonBox_box_left {
					color: #ccc;
					font-size: 34upx;
				}

				.placeOrder_buttonBox_boxNum {
					font-size: 34upx;

					display: flex;
					align-items: center;

					.placeOrder_buttonBox_boxNum_left {
						font-size: 34upx;
						color: #ED2525;
					}

					.placeOrder_buttonBox_boxNum_right {
						font-size: 50upx;
						color: #ED2525;
					}

				}

				.placeOrder_buttonBox_buttonBox {
					margin-left: 32upx;

					.placeOrder_buttonBox_button {
						background-color: #ED2525;
						color: #ffffff;
						border-radius: 174upx;
						width: 174upx;
						height: 74upx;
						line-height: 74upx;

						&::after {
							display: none;
						}

						&[disabled] {
							background-color: #D8D8D8;
							color: #000000;
						}
					}
				}
			}
		}
	}
</style>
<style>
	page {
		background-color: #E5E7E9;
	}
</style>
